<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
include_once("Common.php");
include_once("mysqlha.php");
include_once("UpFileTool.class.php");
class IndexController extends Controller {
    public function index(){
    	
    	$Model = M();
    	//获取广告
	    $adlist = $Model->Query('select * from ad where id>0');
		$classify = $Model->Query('SELECT * FROM classify WHERE is_homepage=1 order by sort');

	    $resultArr = array(
	       "Adlist"=>$adlist,
			"ClassifyList"=>$classify
	    );
	    
	    echo getSuccessJson($resultArr,'查询成功');
    }

    public function login(){
                $this->display("login.php");
    }

   public function UserLogin(){
        $username = $_GET['username'];
        $password = $_GET['password'];
        $Model = M();
        $count = $Model->Query("select count(*) as count from myadmin where username=".$username." and password=".$password."");
        $count =  $count[0]['count'];
        $returnArr = array(
          "code"=>200
        );
        if($count>0){
            echo json_encode($returnArr);
        }
    }

    public function index1(){
	   $this->display("index.html");
    }

    public function present(){
        $this->display("./present/index.html");
    }

    //搜索所有商品
    public function goodList(){

        $page = $_REQUEST["pageNum"];
        $pagecount = $_REQUEST['numPerPage'];
        $keywords = $_REQUEST['keywords'];
        if($page == null||$page<=1){
            $page=1;
        }

        if($pagecount == null||$pagecount<=0){
            $pagecount=20;
        }
        $Model=M();
        $startcount = ($page-1)*$pagecount;
        if($keywords == "") {
            $goods = $Model->Query("select * from goods  order by addtime desc limit " . $startcount . "," . $pagecount . "");
        }else{
            $goods = $Model->Query("select * from goods where name like '".$keywords."%' order by addtime desc limit " . $startcount . "," . $pagecount . "");
        }
        $count = $Model->Query("select count(*) as count from goods");
        $param = array(
          "count"=>$count[0]['count'],
           "numPerPage"=> $pagecount,
            "pageNum"=>$page
        );
        $this->assign('param',$param);
        $this->assign('goods',$goods);
        $this->display("w_list.php");
    }

    //删除一个商品
    public function delOne(){
        $id = $_REQUEST['id'];

        del('goods',"id=".$id."");
        $returnArr = array(
            "code"=>200,
            "msg"=>"删除成功"
        );
        echo json_encode($returnArr);
    }

    //添加商品页面
    public function addGoods(){
        $Model = M();

        $classifyList =$Model->Query("select * from classify");
        $this->assign('classifyList',$classifyList);
        $this->display("addGoods.php");
    }

    //添加商品
    public function addOne(){
        $price = $_REQUEST['price'];
        $name = $_REQUEST['name'];
        $images = $_REQUEST['images'];
        $content = $_REQUEST['content'];
        $numbers = $_REQUEST['numbers'];
        $fk_classify = $_REQUEST['fk_classify'];

        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('pic_master_img');// 实例化上传类

            $UpFileTool->upFile($srcimage='uploads/141');
            $image=$UpFileTool->srcImage;

            $insertArr = array(
                "price"=>$price,
                "name"=>$name,
                "pic_master_img"=>$image,
                "pic_images"=>$images,
                "numbers"=>$numbers,
                "fk_classify"=>$fk_classify,
                "content"=>$content
            );
            myinsert('goods',$insertArr);
            $returnArr = array(
                "code"=>200,
                "callbackType"=>"closeCurrent",
                "navTabId"=>"w_list",
                "forwardUrl"=>"index.php?c=Index&a=goodList",
                "msg"=>"插入成功"
            );
            echo json_encode($returnArr);
        }
    }

    public function setFare(){
        $Model = M();
        $id = $_REQUEST['id'];
        $goodsid = $Model->Query("select muid from goods where id=".$id."");
        $goodsid = $goodsid[0]['muid'];
        $muList = $Model->Query("select * from fareml");
        $this->assign('id',$id);
        $this->assign('muid',$goodsid);
        $this->assign('muList',$muList);
        $this->display('setFare.php');
    }

    public function changeY(){
        $muid = $_REQUEST['muid'];
        $id = $_REQUEST['id'];

        $updateArr = array(
          "muid"=>$muid
        );
        update('goods',$updateArr,"id=".$id."");
        $returnArr = array(
            "code"=>200,
            "callbackType"=>"closeCurrent",
            "navTabId"=>"w_list",
            "forwardUrl"=>"index.php?c=Index&a=goodList",
            "msg"=>"插入成功"
        );
        echo json_encode($returnArr);

    }

    //添加图片
    public function addImage(){
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('image');// 实例化上传类
            $UpFileTool->upFile($srcimage='uploads/141');
            $images=$UpFileTool->srcImage;

            $returnArr = array(
                "code"=>200,
                "multiSumbImage"=>$images,
                "multiSrcImage"=>$images
            );
            echo json_encode($returnArr);
        }
    }

    public function changeGood(){
        $goodsid = $_GET['goodsid'];
        $Model = M();
        $goods = $Model->Query("select * from goods where id=".$goodsid."");
        $pic_images = $goods[0]['pic_images'];
        $Images = explode(',',$pic_images);
        $classifyList =$Model->Query("select * from classify");
        $this->assign('classifyList',$classifyList);
        $this->assign('good',$goods[0]);
        $this->assign('Images',$Images);
        $this->display("changeCoods.php");

    }

    public function ChangeOne(){
        $goodsid = $_REQUEST['goodsid'];
        $name = $_REQUEST['name'];
        $price = $_REQUEST['price'];
        $content = $_REQUEST['content'];
        $numbers = $_REQUEST['numbers'];
        $pic_images = $_REQUEST['images'];
        $fk_classify = $_REQUEST['fk_classify'];
        if ($_FILES) {
            $UpFileTool = new \Think\UpFileTool('pic_master_img');// 实例化上传类
            $UpFileTool->upFile($srcimage='uploads/141');
            $images=$UpFileTool->srcImage;
            if($images == ""){
                $updateArr = array(
                    "name" =>$name,
                    "price"=>$price,
                    "numbers"=>$numbers,
                    "pic_images"=>$pic_images,
                    "fk_classify"=>$fk_classify,
                    "content"=>$content
                );
            }else {
                $updateArr = array(
                    "name" => $name,
                    "price" => $price,
                    "numbers" => $numbers,
                    "pic_images" => $pic_images,
                    "pic_master_img" => $images,
                    "fk_classify" => $fk_classify,
                    "content"=>$content
                );
            }
            update('goods',$updateArr,"id=".$goodsid."");
            $returnArr = array(
                "code"=>200,
                "callbackType"=>"closeCurrent",
                "navTabId"=>"w_list",
                "forwardUrl"=>"index.php?c=Index&a=goodList",
                "msg"=>"插入成功"
            );
            echo json_encode($returnArr);
        }
    }

    public function addPropery(){
        $goodsid = $_REQUEST['goodsid'];
        $price = $_REQUEST['price'];
        $fTitle = $_REQUEST['fTitle'];
        $proid = $_REQUEST['proid'];
        $Model = M();
        for($i=0;$i<sizeof($price);$i++){
            $count = $Model->Query("select count(*) as count from goodselect where goodsid=".$goodsid." and proid=".$proid[$i]." and fTitle='".$fTitle[$i]."'");
            $count =$count[0]['count'];
            if($count == 0){
                $insertArr = array(
                    "price"=>$price[$i],
                    "fTitle" =>$fTitle[$i],
                    "proid"=>$proid[$i],
                    "goodsid"=>$goodsid
                );
                myinsert('goodselect',$insertArr);
            }

        }
        $returnArr = array(
            "code"=>200,
            "callbackType"=>"closeCurrent",
            "navTabId"=>"w_list",
            "forwardUrl"=>"index.php?c=Index&a=goodList",
            "msg"=>"插入成功"
        );
        echo json_encode($returnArr);
    }

    public function changePropery(){
        $goodsid = $_REQUEST['goodsid'];
        $price = $_REQUEST['price'];
        $fTitle = $_REQUEST['fTitle'];
        $proid = $_REQUEST['proid'];
        del('goodselect',"goodsid=".$goodsid."");

        for($i=0;$i<sizeof($price);$i++){
            $insertArr = array(
                "price"=>$price[$i],
                "fTitle" =>$fTitle[$i],
                "proid"=>$proid[$i],
                "goodsid"=>$goodsid
            );
            myinsert('goodselect',$insertArr);
        }
        $returnArr = array(
            "code"=>200,
            "callbackType"=>"closeCurrent",
            "navTabId"=>"w_list",
            "forwardUrl"=>"index.php?c=Index&a=goodList",
            "msg"=>"插入成功"
        );
        echo json_encode($returnArr);

    }

    public function setPropery(){
        $Model = M();
        $classifyId = $_GET['classifyId'];
        $goodsid = $_GET['goodsid'];

        $goods = $Model->Query("select * from clasproty where classifyId=".$classifyId."");
        $ProList = array();
        for($i=0;$i<sizeof($goods);$i++){
            $myrel = $Model->Query("SELECT * FROM goodselect WHERE goodsid=".$goodsid." AND proid=".$goods[$i]['id']."");
            if($myrel) {
                $ProList[$goods[$i]['property']] = $myrel;
            }else{
                $ProList[$goods[$i]['property']] = array(array("proid"=>$goods[$i]['id']));
            }
        }
        $this->assign('ProList',$ProList);
        $this->assign('goodsid',$goodsid);
        $this->display("setPropery.php");

    }
}